<template>
  <div>
    <div class="app-container" style="height: 100%">
      <el-form
        :model="queryParams"
        ref="queryRef"
        :inline="true"
        v-show="showSearch"
      >
        <el-form-item label="方案名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入方案名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          />
        </el-form-item>
        <el-form-item label="方案类型" prop="name">
          <el-select
            v-model="queryParams.planType"
            placeholder="请选择方案名称"
            clearable
            style="width: 200px"
            @keyup.enter="handleQuery"
          >
            <el-option
              v-for="item in typeList"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery"
            >搜索</el-button
          >
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>

      <el-row :gutter="10" class="mb8">
        <right-toolbar
          v-model:showSearch="showSearch"
          @queryTable="getList"
        ></right-toolbar>
      </el-row>

      <el-table v-loading="loading" :data="tableData">
        <el-table-column label="方案名称" width="400px" prop="name" />
        <el-table-column
          label="方案内容"
          width="200px"
          show-overflow-tooltip
          prop="content"
        />
        <el-table-column label="灌区名称" prop="irriId">
          <template #default>万北灌区</template>
        </el-table-column>
        <el-table-column label="计划用水量" prop="totalWater" />
        <el-table-column label="实际用水量" prop="totalActualWater" />
        <el-table-column label="时长" prop="totalTime" />
        <el-table-column label="方案类型" prop="planType">
          <template #default="scope">
            {{ getTypeName(scope.row.planType) }}
          </template>
        </el-table-column>

        <el-table-column
          label="操作"
          align="center"
          width="150px"
          class-name="small-padding fixed-width"
        >
          <template #default="scope">
            <!-- <el-button
              link
              type="primary"
              icon="Edit"
              @click="handleUpdate(scope.row)"
              >修改</el-button
            > -->

            <el-button
              link
              type="primary"
              icon="View"
              @click="handleView(scope.row)"
              >查看详情</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页区域 -->
      <el-pagination
        style="float: right"
        @current-change="handleCurrentChange"
        :current-page="pagination.curPage"
        :page-size="pagination.pageSize"
        layout="total, prev, pager, next"
        :total="pagination.total"
      />
    </div>
    <faDetail ref="faDetailRef" />
  </div>
</template>

<script setup>
import faDetail from "@/views/modal/faDetail.vue";
import { ref, reactive, toRefs, getCurrentInstance } from "vue";
import {
  getModalFaList,
  delModalFa,
  getModalFaDetailById,
  saveModalFa,
} from "@/api/modal/modalfa";
import { ElMessage, ElMessageBox } from "element-plus";
const { proxy } = getCurrentInstance();
const tableData = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(false);
const totalOpen = ref(false);
const title = ref("");
const faDetailRef = ref();
const modalLoading = ref(false);
const totalWater = ref(0);
const pagination = reactive({
  curPage: 1,
  pageSize: 10,
  total: 0,
});
const typeList = reactive([
  { label: "需水模型方案", value: 0 },
  { label: "优化水模型方案", value: 1 },
  { label: "输水模型方案", value: 2 },
]);
const getTypeName = (type) => {
  return typeList.find((item) => item.value === type)?.label;
};
const data = reactive({
  queryParams: {
    name: "",
  },
  rules: {
    name: [{ required: true, message: "设备名称不能为空", trigger: "blur" }],
  },
});
const { queryParams } = toRefs(data);
const handleCurrentChange = (page) => {
  pagination.curPage = page;
  getList();
};
const form = reactive({});
// 更新重置方法，初始化所有字段
function reset() {
  Object.assign(form, {
    id: null,
    name: "",
    profit: 0,
    cost: 0,
    demand: 0,
    maxCapacity: 0,
  });
  proxy.resetForm("formRef");
}

function handleView(row) {
  getModalFaDetailById(row.id).then((res) => {
    faDetailRef.value.openDialog(res.data, "方案详情");
  });
}
/** 查询列表 */
function getList() {
  loading.value = true;
  getModalFaList({ ...pagination, ...queryParams.value }).then((response) => {
    tableData.value = response.data.list;
    pagination.total = response.data.totalCount;
    loading.value = false;
  });
}

/** 取消按钮 */
function cancel() {
  open.value = false;
  reset();
}

/** 搜索按钮操作 */
function handleQuery() {
  getList();
}

/** 重置搜索条件 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

/** 新增按钮 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加地块与模型参数";
}

/** 修改按钮 */
function handleUpdate(row) {
  getModalFaDetailById(row.id).then((res) => {
    Object.assign(form, res.data);
    open.value = true;
    title.value = "修改添加地块与模型参数";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      saveModalFa(form).then(() => {
        proxy.$modal.msgSuccess(form.id ? "修改成功" : "新增成功");
        open.value = false;
        getList();
      });
    }
  });
}

/** 删除按钮 */
function handleDelete(row) {
  ElMessageBox.confirm(
    '是否确认删除名称为"' + row.name + '"的数据项?',
    "提示",
    {
      confirmButtonText: "确定",
      cancelButtonText: "取消",
      type: "warning",
    }
  )
    .then(() => {
      delModalFa([row.id]).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      });
    })
    .catch(() => {
      ElMessage.info("取消删除");
    });
}

getList();
</script>

<style lang="scss" scoped>
:deep(.el-divider__text) {
  font-size: 16px;
  font-weight: bold;
  color: rgb(42, 114, 249);
}
.avatar-uploader .avatar {
  width: 100px;
  height: 100px;
  display: block;
}
.avatar {
  width: 100px;
  height: 100px;
  display: block;
}
.avatar-uploader :deep(.el-upload) {
  border: 1px dashed var(--el-border-color);
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader :deep(.el-upload:hover) {
  border-color: var(--el-color-primary);
}
:deep(.el-icon.avatar-uploader-icon) {
  font-size: 28px;
  color: #8c939d;
  width: 100px;
  height: 100px;
  text-align: center;
}
</style>
